package com.bzkj.web.controller.warehouse;

import cn.hutool.core.date.DateUtil;
import com.bzkj.basicinfo.domain.basic.vo.PrintSkuInfoVo;
import com.bzkj.basicinfo.domain.basic.vo.WarehouseExportVo;
import com.bzkj.basicinfo.domain.ncc.Warehouse;
import com.bzkj.basicinfo.domain.wms.vo.ExportWMSWarehouseOutDetailVo;
import com.bzkj.basicinfo.domain.wms.vo.SkuQueryExportDataVo;
import com.bzkj.basicinfo.service.ncc.WarehouseService;
import com.bzkj.common.annotation.LogC;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.domain.R;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.enums.BusinessType;
import com.bzkj.common.utils.StringUtils;
import com.bzkj.common.utils.poi.CsvExportUtils;
import com.bzkj.common.utils.poi.ExcelPDFUtil;
import com.bzkj.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 仓库控制器，用于管理仓库的增删改查操作。
 * 提供接口用于新增、修改、删除、根据ID查询单个仓库，以及查询所有仓库信息。
 */
@Api(tags = "仓库管理")
@RestController
@RequestMapping("/warehouse")
public class WarehouseController extends BaseController
{
    @Autowired
    private WarehouseService warehouseService;

    /**
     * @param warehouse 新仓库信息
     * @return AjaxResult 返回添加结果信息
     * @method addWarehouse
     * @description 添加新的仓库
     */
    @ApiOperation("添加新的仓库")
    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('index:libraryIndex:add')")
    @LogC(title = "仓库", businessType = BusinessType.INSERT, pkFieldNames = "sto_id",
            tableNames = "basic_sto_stock",
            detailTable = "basic_sto_stock_map",
            paramField = "warehouse.stoName"
    )
    public AjaxResult addWarehouse(@RequestBody Warehouse warehouse)
    {
        return warehouseService.addWarehouse(warehouse);
    }

    /**
     * @param warehouse 更新后的仓库信息
     * @return AjaxResult 返回更新结果信息
     * @method updateWarehouse
     * @description 更新仓库信息
     */
    @ApiOperation("更新仓库信息")
    @PutMapping("/update")
    @PreAuthorize("@ss.hasPermi('index:libraryIndex:edit')")
//    @LogC(title = "仓库管理", businessType = BusinessType.UPDATE, pkFieldNames = "sto_id",
//            tableNames = "basic_sto_stock",
//            detailTable = "basic_sto_stock_map",
//            detailPkField = "id",
//            associatedIdField = "sto_id",content = "'仓库名称：'+#warehouse.stoName"
//    )
    public AjaxResult updateWarehouse(@RequestBody Warehouse warehouse)
    {
        return warehouseService.updateWarehouse(warehouse);
    }

    /**
     * @param stoId 仓库ID
     * @return AjaxResult 返回删除结果信息
     * @method deleteWarehouse
     * @description 根据ID删除仓库
     */
    @ApiOperation("根据stoId删除仓库")
    @DeleteMapping("/{stoId}")
    public AjaxResult deleteWarehouse(@PathVariable String stoId)
    {
        // 检查仓库是否有关联的库区
        if(warehouseService.hasAreas(stoId)) {
            return AjaxResult.error("删除失败：仓库有关联的库区，请先删除库区");
        }
        int result = warehouseService.deleteWarehouse(stoId);
        if(result > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * @param stoId 仓库ID
     * @return AjaxResult 返回仓库信息
     * @method getWarehouseById
     * @description 根据ID获取仓库信息
     */
    @ApiOperation("根据stoId获取仓库信息")
    @GetMapping("/{stoId}")
    public AjaxResult getWarehouseById(@PathVariable String stoId)
    {
        Warehouse warehouse = warehouseService.getWarehouseById(stoId);
        if(warehouse != null) {
            return AjaxResult.success(warehouse);
        } else {
            return AjaxResult.error("仓库未找到");
        }
    }

    /**
     * @param warehouse 查询条件
     * @return TableDataInfo 返回分页后的仓库列表
     * @method getAllWarehouses
     * @description 获取所有仓库信息，带分页功能
     */
    @ApiOperation("获取所有仓库信息,分页")
    @GetMapping("/allList")
    public TableDataInfo getAllWarehouses(Warehouse warehouse)
    {
        startPage();
        List<Warehouse> warehouses = warehouseService.getAllWarehousesList(warehouse);
        return getDataTable(warehouses);
    }

    @ApiOperation("获取所有仓库信息,不分页")
    @GetMapping("getAllWarehouseList")
    public AjaxResult getAllWarehousesList(Warehouse warehouse) {
        List<Warehouse> warehouses = warehouseService.getAllWarehouses(warehouse);
        return AjaxResult.success(warehouses);
    }

    /**
     * 在新增的时候，前端显示ncc中的仓库name，和新增确定的时候给后端传递pk_sto_doc
     *
     * @return
     */
    @ApiOperation("新增查询，获取ncc中的仓库name和pk_sto_doc，也就是label和value")
    @GetMapping("/nccBasicStoStock")
    public AjaxResult getNccBasicStoStock()
    {
        return warehouseService.getNccBasicStoStock();
    }

    @PreAuthorize("@ss.hasPermi('index:wareHouse:list')")
    @GetMapping("/all")
    public HashMap<String, Object> queryStoWareHouse(String sto_code)
    {
        HashMap<String, Object> response = new HashMap<>();
        if(sto_code == null) {
            sto_code = "";
        }
        //        调用函数直接返回
        List<HashMap<String, Object>> hashMaps = null;
        try {
            try {
                hashMaps = warehouseService.queryStoWareHouse(sto_code);
                response.put("success", true);
                response.put("data", hashMaps);
                // 添加其他字段
                response.put("code", "200");
                response.put("message", "库房查询成功");
                response.put("errorStack", null);
                response.put("totalnum", hashMaps.size());
            } catch(Exception e) {
                response.put("success", false);
                response.put("code", "1000000001");
                response.put("message", "库房查询异常");
                response.put("data", null);
                response.put("errorStack", e);
                response.put("totalnum", 0);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 修改仓库状态
     *
     * @Date 2024/12/12 13:33:23
     * @Param status
     * @Param stoId
     */
    @PutMapping("updateEnableStatus")
    public AjaxResult updateEnableStatus(@RequestParam String status, @RequestParam String stoId)
    {
        return warehouseService.updateEnableStatus(status, stoId);
    }

    /**
     * 获取入库下拉参数
     *
     * @param dataType
     * @param SType
     * @return
     */
    //    @PreAuthorize("@ss.hasPermi('report:buyInbound:list')")
    @GetMapping("getWarehouseInBasic")
    @ApiOperation("查询参数下拉接口")
    public R getWarehouseInBasic(@RequestParam(required = false, defaultValue = "1") String dataType, @RequestParam(required = false) String SType, @RequestParam(required = false, defaultValue = "") String wareHouseMode)
    {
        return warehouseService.getWarehouseInBasic(dataType, SType, wareHouseMode);
    }

    /**
     * 根据日期获取入库数据
     *
     * @param dataType
     * @return
     */
    //    @PreAuthorize("@ss.hasPermi('report:buyInbound:list')")
    @GetMapping("getWarehouseInNoFinishBills")
    @ApiOperation("入库单据查询接口")
    public R getWarehouseInNoFinishBills(@RequestParam(required = false, defaultValue = "1") String dataType, @RequestParam(required = false, defaultValue = "") String wareHouseMode, @RequestParam(required = false) String noFinishMode)
    {
        return warehouseService.getWarehouseInNoFinishBills(dataType, wareHouseMode, noFinishMode);
    }

    /**
     * 查询入库统计数据
     *
     * @param dataType
     * @param wareHouseMode
     * @param wareHouseModeValue
     * @param wareHouseName
     * @param wareHouseValue
     * @param senderId
     * @param senderValue
     * @param SkuId
     * @param SkuIdValue
     * @param SType
     * @return
     */
    //    @PreAuthorize("@ss.hasPermi('report:buyInbound:list')")
    @GetMapping("getWarehouseInStatistic")
    @ApiOperation("入库统计查询")
    public R getWarehouseInStatistic(@RequestParam(required = false, defaultValue = "1") String dataType,
                                     @RequestParam(required = false, defaultValue = "") String wareHouseMode,
                                     @RequestParam(required = false, defaultValue = "") String wareHouseModeValue,
                                     @RequestParam(required = false, defaultValue = "") String wareHouseName,
                                     @RequestParam(required = false, defaultValue = "") String wareHouseValue,
                                     @RequestParam(required = false, defaultValue = "") String senderId,
                                     @RequestParam(required = false, defaultValue = "") String senderValue,
                                     @RequestParam(required = false, defaultValue = "") String SkuId,
                                     @RequestParam(required = false, defaultValue = "") String SkuIdValue,
                                     @RequestParam(required = false) String SType
    )
    {
        return warehouseService.getWarehouseInStatistic(dataType, wareHouseMode, wareHouseModeValue, wareHouseName, wareHouseValue, senderId, senderValue, SkuId, SkuIdValue, SType);
    }


    /**
     * 出库下拉框数据
     *
     * @param dataType
     * @param SType
     * @param wareHouseMode
     * @return
     */
    //    @PreAuthorize("@ss.hasPermi('report:arriveList:list')")
    @GetMapping("getWarehouseOutBasic")
    @ApiOperation("出库单据下拉参数接口")
    public R getWarehouseOutBasic(@RequestParam(required = false, defaultValue = "1") String dataType, @RequestParam(required = false) String SType, @RequestParam(required = false, defaultValue = "") String wareHouseMode)
    {
        return warehouseService.getWarehouseOutBasic(dataType, SType, wareHouseMode);
    }

    /**
     * 根据日期获取出库数据
     *
     * @param dataType
     * @return
     */
    //    @PreAuthorize("@ss.hasPermi('report:arriveList:list')")
    @GetMapping("getWarehouseOutNoFinishBills")
    @ApiOperation("出库单据查询接口")
    public R getWarehouseOutNoFinishBills(@RequestParam(required = false, defaultValue = "1") String dataType, @RequestParam(required = false, defaultValue = "") String wareHouseMode, @RequestParam(required = false, defaultValue = "") String noFinishMode)
    {
        return warehouseService.getWarehouseOutNoFinishBills(dataType, wareHouseMode, noFinishMode);
    }

    /**
     * 出库统计报表数据
     *
     * @param dataType
     * @param wareHouseMode
     * @param wareHouseModeValue
     * @param wareHouseName
     * @param wareHouseValue
     * @param customerId
     * @param senderValue
     * @param SkuId
     * @param SkuIdValue
     * @param SType
     * @return
     */
    //    @PreAuthorize("@ss.hasPermi('report:arriveList:list')")
    @GetMapping("getWarehouseOutStatistic")
    @ApiOperation("出库单据统计接口")
    public R getWarehouseOutStatistic(@RequestParam(required = false, defaultValue = "1") String dataType,
                                      @RequestParam(required = false, defaultValue = "") String wareHouseMode,
                                      @RequestParam(required = false, defaultValue = "") String wareHouseModeValue,
                                      @RequestParam(required = false, defaultValue = "") String wareHouseName,
                                      @RequestParam(required = false, defaultValue = "") String wareHouseValue,
                                      @RequestParam(required = false, defaultValue = "") String customerId,
                                      @RequestParam(required = false, defaultValue = "") String senderValue,
                                      @RequestParam(required = false, defaultValue = "") String SkuId,
                                      @RequestParam(required = false, defaultValue = "") String SkuIdValue,
                                      @RequestParam(required = false) String SType
    )
    {
        return warehouseService.getWarehouseOutStatistic(dataType, wareHouseMode, wareHouseModeValue, wareHouseName, wareHouseValue, customerId, senderValue, SkuId, SkuIdValue, SType);
    }


    /**
     * 生命周期管控
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('report:lifeCycle:list')")
    @GetMapping("findAuditBills")
    public R findAuditBills(@RequestParam(required = false) String billsID)
    {
        return warehouseService.findAuditBills(billsID);
    }

    /**
     * 生命周期管控
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('report:lifeCycle:list')")
    @GetMapping("findAuditSku")
    public R findAuditSku(@RequestParam(required = false) String skuId,
                          @RequestParam(required = false, defaultValue = "") String batchNo,
                          @RequestParam(required = false, defaultValue = "") String barCode,
                          @RequestParam(required = false) String beginDate,
                          @RequestParam(required = false) String endDate,
                          Integer pageNum,Integer pageSize
    )
    {
        return warehouseService.findAuditSku(skuId, batchNo, barCode, beginDate, endDate,pageNum,pageSize);
    }

    /**
     * 生命周期管控
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('report:lifeCycle:list')")
    @GetMapping("findAuditUser")
    public R findAuditUser(@RequestParam(required = false) String userId, @RequestParam(required = false) String beginDate, @RequestParam(required = false) String endDate,Integer pageNum,Integer pageSize)
    {
        return warehouseService.findAuditUser(userId, beginDate, endDate,pageNum,pageSize);
    }

    /**
     * 仓库统计报表数据
     *
     * @param stoCode
     * @return
     */
    //    @PreAuthorize("@ss.hasPermi('basic:bankStat:list')")
    @GetMapping("findStatisticSto")
    public R findStatisticSto(@RequestParam(required = false, defaultValue = "") String stoCode)
    {
        return warehouseService.findStatisticSto(stoCode);
    }

    /**
     * 仓库单元查询
     *
     * @param stoCode
     * @param unitIdValue
     * @param unitId
     * @return
     */
    //    @PreAuthorize("@ss.hasPermi('basic:bankStat:list')")
    @GetMapping("getStoUnitQuery")
    public R getStoUnitQuery(@RequestParam(required = false) String stoCode, @RequestParam(required = false) String unitIdValue, @RequestParam(required = false) String unitId)
    {
        return warehouseService.getStoUnitQuery(stoCode, unitIdValue, unitId);
    }

    /**
     * 告警查询
     *
     * @param alarmsMode
     * @param alarmsValues
     * @return
     */
    @ApiOperation("库存预警查询接口")
    @GetMapping("getSkuAlarms")
    public R getSkuAlarms(@RequestParam(required = false) String alarmsMode,
                          @RequestParam(required = false) String vbatchNo,
                          @RequestParam(required = false) String drugName,
                          @RequestParam(required = false) String startDate,
                          @RequestParam(required = false) String endDate,
                          @RequestParam(required = false) String alarmsValues,
                          Integer pageNum, Integer pageSize) {
        return warehouseService.getSkuAlarms(alarmsMode, vbatchNo, drugName, startDate, endDate, alarmsValues, pageNum, pageSize);
    }

    @ApiOperation("库存预警查询导出")
    @LogC(title = "库存预警查询", businessType = BusinessType.EXPORT)
    @PostMapping("exportSkuAlarms")
    public AjaxResult exportSkuAlarms(@RequestParam(required = false) String alarmsMode,
                                      @RequestParam(required = false) String vbatchNo,
                                      @RequestParam(required = false) String drugName,
                                      @RequestParam(required = false) String startDate,
                                      @RequestParam(required = false) String endDate,
                                      @RequestParam(required = false) String alarmsValues,
                                      Integer pageNum, Integer pageSize, HttpServletResponse response) {
        return warehouseService.exportSkuAlarms(alarmsMode, vbatchNo, drugName, startDate, endDate, alarmsValues,  pageNum, pageSize, response);
    }

    /**
     * 出库明细打印
     *
     * @param cgeneralbid
     * @return
     */
    @PostMapping("getWarehouseOutDetailProductPrint")
    public R getWarehouseOutDetailProductPrint(@RequestBody List<String> cgeneralbid)
    {
        return warehouseService.getWarehouseOutDetailProductPrint(cgeneralbid);
    }

    /**
     * 仓库单元统计报表数据
     *
     * @param stoAreaCode
     * @param stoUnitCode
     * @return
     */
    //    @PreAuthorize("@ss.hasPermi('basic:bankStat:list')")
    @GetMapping("getStatisticStoUnit")
    public R getStatisticStoUnit(@RequestParam(required = false, defaultValue = "") String stoAreaCode, @RequestParam(required = false, defaultValue = "") String stoUnitCode)
    {
        return warehouseService.getStatisticStoUnit(stoAreaCode, stoUnitCode);
    }

    /**
     * sku查询
     *
     * @param skuId
     * @param batchNoMode
     * @param pkBatchNo
     * @return
     */
    @ApiOperation("查询接口")
    @PreAuthorize("@ss.hasPermi('basic:bankStat:list') || @ss.hasPermi('organize:stockQuery:query') || @ss.hasPermi('storeM:storeShift:query')")
    @GetMapping("getSkuQuery")
    public R getSkuQuery(@RequestParam(required = false, defaultValue = "") String skuId,
                         @RequestParam(required = false, defaultValue = "") String batchNoMode,
                         @RequestParam(required = false, defaultValue = "") String pkBatchNo)
    {
        return R.ok((warehouseService.getSkuQuery(skuId, batchNoMode, pkBatchNo)));
    }

    /**
     * 仓库统计报表数据
     *
     * @param skuclassId
     * @param wareHouseID
     * @param wareHouseValue
     * @param skuID
     * @param skuIDValue
     * @param batchNo
     * @param batchNoValue
     * @param expDateNum
     * @param expDateValue
     * @param resetDateNum
     * @param resetDateValue
     * @param stockNum
     * @param stocValue
     * @param skuState
     * @param sType
     * @return
     */
    //@PreAuthorize("hasAnyAuthority('report:bankStat:list','organize:verifyOrg:list','organize:goodsOrg:list','storeM:storeCheck:query')")
    //@PreAuthorize("@ss.hasPermi('report:bankStat:list') || @ss.hasPermi('organize:verifyOrg:list') || @ss.hasPermi('organize:goodsOrg:list') || @ss.hasPermi('storeM:storeCheck:query')")
    @ApiOperation("库存整体统计查询接口")
    @GetMapping("getWarehouseStatistic")
    public R getWarehouseStatistic(
            @RequestParam(required = false, defaultValue = "") String skuclassId,
            @RequestParam(required = false, defaultValue = "") String skuclassValue,
            @RequestParam(required = false, defaultValue = "") String wareHouseID,
            @RequestParam(required = false, defaultValue = "") String wareHouseValue,
            @RequestParam(required = false, defaultValue = "") String skuID,
            @RequestParam(required = false, defaultValue = "") String skuIDValue,
            @RequestParam(required = false, defaultValue = "") String batchNo,
            @RequestParam(required = false, defaultValue = "") String batchNoValue,
            @RequestParam(required = false, defaultValue = "") String expDateNum,
            @RequestParam(required = false, defaultValue = "") String expDateValue,
            @RequestParam(required = false, defaultValue = "") String resetDateNum,
            @RequestParam(required = false, defaultValue = "") String resetDateValue,
            @RequestParam(required = false, defaultValue = "") String stockNum,
            @RequestParam(required = false, defaultValue = "") String stocValue,
            @RequestParam(required = false, defaultValue = "") String skuState,
            @RequestParam(required = false, defaultValue = "") String skuStateValue,
            @RequestParam(required = false, defaultValue = "") String sType
    )
    {
        return warehouseService.getWarehouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, sType);
    }

    /**
     * 请验管理数据接口
     * @param skuclassId
     * @param skuclassValue
     * @param wareHouseID
     * @param wareHouseValue
     * @param skuID
     * @param skuIDValue
     * @param batchNo
     * @param batchNoValue
     * @param expDateNum
     * @param expDateValue
     * @param resetDateNum
     * @param resetDateValue
     * @param stockNum
     * @param stocValue
     * @param skuState
     * @param skuStateValue
     * @param sType
     * @return
     */
    @ApiOperation("请验管理查询接口")
    @GetMapping("getWarehouseStatistics")
    public R getWarehouseStatistics(
            @RequestParam(required = false, defaultValue = "") String skuclassId,
            @RequestParam(required = false, defaultValue = "") String skuclassValue,
            @RequestParam(required = false, defaultValue = "") String wareHouseID,
            @RequestParam(required = false, defaultValue = "") String wareHouseValue,
            @RequestParam(required = false, defaultValue = "") String skuID,
            @RequestParam(required = false, defaultValue = "") String skuIDValue,
            @RequestParam(required = false, defaultValue = "") String batchNo,
            @RequestParam(required = false, defaultValue = "") String batchNoValue,
            @RequestParam(required = false, defaultValue = "") String expDateNum,
            @RequestParam(required = false, defaultValue = "") String expDateValue,
            @RequestParam(required = false, defaultValue = "") String resetDateNum,
            @RequestParam(required = false, defaultValue = "") String resetDateValue,
            @RequestParam(required = false, defaultValue = "") String stockNum,
            @RequestParam(required = false, defaultValue = "") String stocValue,
            @RequestParam(required = false, defaultValue = "") String skuState,
            @RequestParam(required = false, defaultValue = "") String skuStateValue,
            @RequestParam(required = false, defaultValue = "") String sType
    )
    {
        return warehouseService.getWarehouseStatistics(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, sType);
    }

    @ApiOperation("库存整体统计导出excel")
    @PostMapping("/exportWareHouseStatistic")
    @LogC(title = "库存整体统计",businessType = BusinessType.EXPORT)
    public AjaxResult exportWareHouseStatistic(
            @RequestParam(required = false, defaultValue = "") String skuclassId,
            @RequestParam(required = false, defaultValue = "") String skuclassValue,
            @RequestParam(required = false, defaultValue = "") String wareHouseID,
            @RequestParam(required = false, defaultValue = "") String wareHouseValue,
            @RequestParam(required = false, defaultValue = "") String skuID,
            @RequestParam(required = false, defaultValue = "") String skuIDValue,
            @RequestParam(required = false, defaultValue = "") String batchNo,
            @RequestParam(required = false, defaultValue = "") String batchNoValue,
            @RequestParam(required = false, defaultValue = "") String expDateNum,
            @RequestParam(required = false, defaultValue = "") String expDateValue,
            @RequestParam(required = false, defaultValue = "") String resetDateNum,
            @RequestParam(required = false, defaultValue = "") String resetDateValue,
            @RequestParam(required = false, defaultValue = "") String stockNum,
            @RequestParam(required = false, defaultValue = "") String stocValue,
            @RequestParam(required = false, defaultValue = "") String skuState,
            @RequestParam(required = false, defaultValue = "") String skuStateValue,
            Integer pageNum, Integer pageSize,
            HttpServletResponse response
    ) {
        List<WarehouseExportVo> list = warehouseService.exportWareHouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, pageNum,pageSize,response);
        list.forEach(i -> {
            if(StringUtils.isNotBlank(i.getRetest_date())){
                i.setRetest_date(DateUtil.format(DateUtil.parse(i.getRetest_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
            if(StringUtils.isNotBlank(i.getExp_date())){
                i.setExp_date(DateUtil.format(DateUtil.parse(i.getExp_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
            if(StringUtils.isNotBlank(i.getPd_date())){
                i.setPd_date(DateUtil.format(DateUtil.parse(i.getPd_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
        });
        ExcelUtil<WarehouseExportVo> util = new ExcelUtil<>(WarehouseExportVo.class);
        util.exportExcel(response, list, "库存整体统计", "库存整体统计", "库存整体统计");
        return AjaxResult.success();
    }

    @ApiOperation("库存整体统计导出pdf")
    @PostMapping("/exportPdfWareHouseStatistic")
    @LogC(title = "库存整体统计", businessType = BusinessType.EXPORTPDF)
    public AjaxResult exportPdfWareHouseStatistic(
            @RequestParam(required = false, defaultValue = "") String skuclassId,
            @RequestParam(required = false, defaultValue = "") String skuclassValue,
            @RequestParam(required = false, defaultValue = "") String wareHouseID,
            @RequestParam(required = false, defaultValue = "") String wareHouseValue,
            @RequestParam(required = false, defaultValue = "") String skuID,
            @RequestParam(required = false, defaultValue = "") String skuIDValue,
            @RequestParam(required = false, defaultValue = "") String batchNo,
            @RequestParam(required = false, defaultValue = "") String batchNoValue,
            @RequestParam(required = false, defaultValue = "") String expDateNum,
            @RequestParam(required = false, defaultValue = "") String expDateValue,
            @RequestParam(required = false, defaultValue = "") String resetDateNum,
            @RequestParam(required = false, defaultValue = "") String resetDateValue,
            @RequestParam(required = false, defaultValue = "") String stockNum,
            @RequestParam(required = false, defaultValue = "") String stocValue,
            @RequestParam(required = false, defaultValue = "") String skuState,
            @RequestParam(required = false, defaultValue = "") String skuStateValue,
            Integer pageNum, Integer pageSize,
            HttpServletResponse response
    ) {
        List<WarehouseExportVo> list = warehouseService.exportWareHouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue,pageNum,pageSize, response);
        list.forEach(i -> {
            if(StringUtils.isNotBlank(i.getRetest_date())){
                i.setRetest_date(DateUtil.format(DateUtil.parse(i.getRetest_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
            if(StringUtils.isNotBlank(i.getExp_date())){
                i.setExp_date(DateUtil.format(DateUtil.parse(i.getExp_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
            if(StringUtils.isNotBlank(i.getPd_date())){
                i.setPd_date(DateUtil.format(DateUtil.parse(i.getPd_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
        });
        ExcelPDFUtil<WarehouseExportVo> util = new ExcelPDFUtil<>(WarehouseExportVo.class);
        return util.exportPDF(response, list, "库存整体统计", "库存整体统计", getUsername(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), "全量");
    }

    @ApiOperation("库存整体统计导出csv")
    @PostMapping("/exportCsvWareHouseStatistic")
    @LogC(title = "库存整体统计", businessType = BusinessType.EXPORTCSV)
    public AjaxResult exportCsvWareHouseStatistic(
            @RequestParam(required = false, defaultValue = "") String skuclassId,
            @RequestParam(required = false, defaultValue = "") String skuclassValue,
            @RequestParam(required = false, defaultValue = "") String wareHouseID,
            @RequestParam(required = false, defaultValue = "") String wareHouseValue,
            @RequestParam(required = false, defaultValue = "") String skuID,
            @RequestParam(required = false, defaultValue = "") String skuIDValue,
            @RequestParam(required = false, defaultValue = "") String batchNo,
            @RequestParam(required = false, defaultValue = "") String batchNoValue,
            @RequestParam(required = false, defaultValue = "") String expDateNum,
            @RequestParam(required = false, defaultValue = "") String expDateValue,
            @RequestParam(required = false, defaultValue = "") String resetDateNum,
            @RequestParam(required = false, defaultValue = "") String resetDateValue,
            @RequestParam(required = false, defaultValue = "") String stockNum,
            @RequestParam(required = false, defaultValue = "") String stocValue,
            @RequestParam(required = false, defaultValue = "") String skuState,
            @RequestParam(required = false, defaultValue = "") String skuStateValue,
            Integer pageNum, Integer pageSize,
            HttpServletResponse response
    ) {
        List<WarehouseExportVo> warehouseExportVos = warehouseService.exportWareHouseStatistic(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, pageNum,pageSize,response);
        warehouseExportVos.forEach(i -> {
            if(StringUtils.isNotBlank(i.getRetest_date())){
                i.setRetest_date(DateUtil.format(DateUtil.parse(i.getRetest_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
            if(StringUtils.isNotBlank(i.getExp_date())){
                i.setExp_date(DateUtil.format(DateUtil.parse(i.getExp_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
            if(StringUtils.isNotBlank(i.getPd_date())){
                i.setPd_date(DateUtil.format(DateUtil.parse(i.getPd_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
        });
        try {
            CsvExportUtils.exportToWeb(response, "库存整体统计.csv", warehouseExportVos);
            return AjaxResult.success("导出成功");
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("导出失败");
        }
    }

    /**
     * 商品下拉框数据
     *
     * @param sType
     * @return
     */
    //    @PreAuthorize("@ss.hasPermi('report:bankStat:list')")
    @ApiOperation("查询商品分类、仓库和产品接口")
    @GetMapping("getWarehouseBasic")
    public R getWarehouseBasic(@RequestParam(required = false, defaultValue = "") String sType)
    {
        return warehouseService.getWarehouseBasic(sType);
    }

    /**
     * 获取单据状态
     *
     * @return
     */
    @ApiOperation("查询商品状态接口")
    @GetMapping("getBillStatusAll")
    public R getBillStatusAll()
    {
        return warehouseService.getBillStatusAll();
    }

    /**
     * 商品周转率统计
     *
     * @return
     */
    //    @PreAuthorize("@ss.hasPermi('report:turnover:list')")
    @ApiOperation("库存周转率查询接口")
    @GetMapping("findTurnoverStatistic")
    public R findTurnoverStatistic(
            @RequestParam(required = false, defaultValue = "") String beginDate,
            @RequestParam(required = false, defaultValue = "") String rndDate,
            @RequestParam(required = false, defaultValue = "") String skuClass,
            @RequestParam(required = false, defaultValue = "") String skuClassValue,
            @RequestParam(required = false, defaultValue = "") String wareHouseName,
            @RequestParam(required = false, defaultValue = "") String wareHouseValue,
            @RequestParam(required = false, defaultValue = "") String skuID,
            @RequestParam(required = false, defaultValue = "") String skuIDValue,
            @RequestParam(required = false, defaultValue = "") String batchNo,
            @RequestParam(required = false, defaultValue = "") String batchNoValue,
            @RequestParam(required = false, defaultValue = "") String skuState,
            @RequestParam(required = false, defaultValue = "") String sType
    )
    {
        return warehouseService.findTurnoverStatistic(beginDate, rndDate, skuClass, skuClassValue, wareHouseName, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, skuState, sType);
    }

    /**
     * 库存盘点数据导出为excel
     *
     * @param response
     */
    @ApiOperation("导出excel接口")
    @PreAuthorize("@ss.hasPermi('basic:bankStat:list') || @ss.hasPermi('storeM:storeCheck:export')")
    @PostMapping("skuQueryExportData")
    @LogC(title = "库存盘点数据", businessType = BusinessType.EXPORT)
    public AjaxResult skuQueryExportData(@RequestParam(required = false, defaultValue = "") String skuclassId,
                                   @RequestParam(required = false, defaultValue = "") String skuclassValue,
                                   @RequestParam(required = false, defaultValue = "") String wareHouseID,
                                   @RequestParam(required = false, defaultValue = "") String wareHouseValue,
                                   @RequestParam(required = false, defaultValue = "") String skuID,
                                   @RequestParam(required = false, defaultValue = "") String skuIDValue,
                                   @RequestParam(required = false, defaultValue = "") String batchNo,
                                   @RequestParam(required = false, defaultValue = "") String batchNoValue,
                                   @RequestParam(required = false, defaultValue = "") String expDateNum,
                                   @RequestParam(required = false, defaultValue = "") String expDateValue,
                                   @RequestParam(required = false, defaultValue = "") String resetDateNum,
                                   @RequestParam(required = false, defaultValue = "") String resetDateValue,
                                   @RequestParam(required = false, defaultValue = "") String stockNum,
                                   @RequestParam(required = false, defaultValue = "") String stocValue,
                                   @RequestParam(required = false, defaultValue = "") String skuState,
                                   @RequestParam(required = false, defaultValue = "") String skuStateValue,
                                   @RequestParam(required = false, defaultValue = "4") String sType,
                                   Integer pageNum,Integer pageSize,
                                   HttpServletResponse response)
    {
        List<SkuQueryExportDataVo> list = warehouseService.skuQueryExportData(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, sType,pageNum,pageSize);
        list.forEach(i -> {
            if(StringUtils.isNotBlank(i.getRetest_date())){
                i.setRetest_date(DateUtil.format(DateUtil.parse(i.getRetest_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
            if(StringUtils.isNotBlank(i.getExp_date())){
                i.setExp_date(DateUtil.format(DateUtil.parse(i.getExp_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
            if(StringUtils.isNotBlank(i.getPd_date())){
                i.setPd_date(DateUtil.format(DateUtil.parse(i.getPd_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
        });
        ExcelUtil<SkuQueryExportDataVo> util = new ExcelUtil<>(SkuQueryExportDataVo.class);
        util.exportExcel(response, list, "库存盘点数据", "库存盘点数据", "skuQuery");
        return AjaxResult.success();
    }
    /**
     * 库存盘点数据导出为pdf
     *
     * @param response
     */
    @ApiOperation("导出pdf接口")
    @PreAuthorize("@ss.hasPermi('basic:bankStat:list') || @ss.hasPermi('storeM:storeCheck:export')")
    @PostMapping("skuQueryExportPdfData")
    @LogC(title = "库存盘点数据", businessType = BusinessType.EXPORTPDF)
    public AjaxResult skuQueryExportPdfData(@RequestParam(required = false, defaultValue = "") String skuclassId,
                                   @RequestParam(required = false, defaultValue = "") String skuclassValue,
                                   @RequestParam(required = false, defaultValue = "") String wareHouseID,
                                   @RequestParam(required = false, defaultValue = "") String wareHouseValue,
                                   @RequestParam(required = false, defaultValue = "") String skuID,
                                   @RequestParam(required = false, defaultValue = "") String skuIDValue,
                                   @RequestParam(required = false, defaultValue = "") String batchNo,
                                   @RequestParam(required = false, defaultValue = "") String batchNoValue,
                                   @RequestParam(required = false, defaultValue = "") String expDateNum,
                                   @RequestParam(required = false, defaultValue = "") String expDateValue,
                                   @RequestParam(required = false, defaultValue = "") String resetDateNum,
                                   @RequestParam(required = false, defaultValue = "") String resetDateValue,
                                   @RequestParam(required = false, defaultValue = "") String stockNum,
                                   @RequestParam(required = false, defaultValue = "") String stocValue,
                                   @RequestParam(required = false, defaultValue = "") String skuState,
                                   @RequestParam(required = false, defaultValue = "") String skuStateValue,
                                   @RequestParam(required = false, defaultValue = "4") String sType,
                                            Integer pageNum,Integer pageSize,
                                   HttpServletResponse response) throws Exception {
        List<SkuQueryExportDataVo> list = warehouseService.skuQueryExportData(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, sType,pageNum,pageSize);
        list.forEach(i -> {
            if(StringUtils.isNotBlank(i.getRetest_date())){
                i.setRetest_date(DateUtil.format(DateUtil.parse(i.getRetest_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
            if(StringUtils.isNotBlank(i.getExp_date())){
                i.setExp_date(DateUtil.format(DateUtil.parse(i.getExp_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
            if(StringUtils.isNotBlank(i.getPd_date())){
                i.setPd_date(DateUtil.format(DateUtil.parse(i.getPd_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
        });
        ExcelPDFUtil<SkuQueryExportDataVo> pdfUtil = new ExcelPDFUtil<>(SkuQueryExportDataVo.class);
        return pdfUtil.exportPDF(response, list, "库存盘点", "库存盘点", getUsername(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),"全量");
    }
    /**
     * 库存盘点数据导出为csv
     *
     * @param response
     */
    @PreAuthorize("@ss.hasPermi('basic:bankStat:list') || @ss.hasPermi('storeM:storeCheck:export')")
    @PostMapping("skuQueryExportCsvData")
    @LogC(title = "库存盘点数据", businessType = BusinessType.EXPORTCSV)
    public AjaxResult skuQueryExportCsvData(@RequestParam(required = false, defaultValue = "") String skuclassId,
                                   @RequestParam(required = false, defaultValue = "") String skuclassValue,
                                   @RequestParam(required = false, defaultValue = "") String wareHouseID,
                                   @RequestParam(required = false, defaultValue = "") String wareHouseValue,
                                   @RequestParam(required = false, defaultValue = "") String skuID,
                                   @RequestParam(required = false, defaultValue = "") String skuIDValue,
                                   @RequestParam(required = false, defaultValue = "") String batchNo,
                                   @RequestParam(required = false, defaultValue = "") String batchNoValue,
                                   @RequestParam(required = false, defaultValue = "") String expDateNum,
                                   @RequestParam(required = false, defaultValue = "") String expDateValue,
                                   @RequestParam(required = false, defaultValue = "") String resetDateNum,
                                   @RequestParam(required = false, defaultValue = "") String resetDateValue,
                                   @RequestParam(required = false, defaultValue = "") String stockNum,
                                   @RequestParam(required = false, defaultValue = "") String stocValue,
                                   @RequestParam(required = false, defaultValue = "") String skuState,
                                   @RequestParam(required = false, defaultValue = "") String skuStateValue,
                                   @RequestParam(required = false, defaultValue = "4") String sType,
                                   Integer pageNum,Integer pageSize,
                                   HttpServletResponse response) {
        List<SkuQueryExportDataVo> list = warehouseService.skuQueryExportData(skuclassId, skuclassValue, wareHouseID, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue, expDateNum, expDateValue, resetDateNum, resetDateValue, stockNum, stocValue, skuState, skuStateValue, sType,pageNum,pageSize);
        list.forEach(i -> {
            if(StringUtils.isNotBlank(i.getRetest_date())){
                i.setRetest_date(DateUtil.format(DateUtil.parse(i.getRetest_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
            if(StringUtils.isNotBlank(i.getExp_date())){
                i.setExp_date(DateUtil.format(DateUtil.parse(i.getExp_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
            if(StringUtils.isNotBlank(i.getPd_date())){
                i.setPd_date(DateUtil.format(DateUtil.parse(i.getPd_date(), "yyyy-MM-dd"),"yyyy-MM-dd"));
            }
        });
        try {
            CsvExportUtils.exportToWeb(response,"库存盘点数据.csv",list);
            return AjaxResult.success("导出成功");
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("导出失败");
        }
    }

    /**
     * 人员kpi信息
     *
     * @param dataType
     * @param sType
     * @return
     */
    @GetMapping("wmsUserKpi")
    public R wmsUserKpi(@RequestParam(required = false) String dataType, @RequestParam(required = false) String sType)
    {
        return warehouseService.wmsUserKpi(dataType, sType);
    }


    /**
     * 出库明细
     *
     * @param beginDate 开始日期
     * @param endDate 结束日期
     * @param skuClass 商品分类
     * @param skuClassValue 商品分类筛选条件
     * @param wareHouseName 仓库id
     * @param wareHouseValue 仓库id筛选条件
     * @param skuID 商品id
     * @param skuIDValue 商品id筛选条件
     * @param batchNo 批次编号
     * @param batchNoValue 批次编号筛选条件
     * @return
     */
    @ApiOperation("查询接口")
    @GetMapping("getWMSWarehouseDetail")
    public R getWMSWarehouseDetail(
            @RequestParam(required = false, defaultValue = "") String beginDate,
            @RequestParam(required = false, defaultValue = "") String endDate,
            @RequestParam(required = false, defaultValue = "") String skuClass,
            @RequestParam(required = false, defaultValue = "") String skuClassValue,
            @RequestParam(required = false, defaultValue = "") String wareHouseName,
            @RequestParam(required = false, defaultValue = "") String wareHouseValue,
            @RequestParam(required = false, defaultValue = "") String skuID,
            @RequestParam(required = false, defaultValue = "") String skuIDValue,
            @RequestParam(required = false, defaultValue = "") String batchNo,
            @RequestParam(required = false, defaultValue = "") String batchNoValue
    )
    {
        return warehouseService.getWMSWarehouseDetail(beginDate, endDate, skuClass, skuClassValue, wareHouseName, wareHouseValue, skuID, skuIDValue, batchNo, batchNoValue);
    }

    /**
     * 出库明细导出
     * @param beginDate
     * @param endDate
     * @param skuClass
     * @param skuClassValue
     * @param wareHouseName
     * @param wareHouseValue
     * @param skuId
     * @param skuIdValue
     * @param batchNo
     * @param batchNoValue
     * @param response
     */
    @ApiOperation("导出excel接口")
    @PostMapping("exportWMSWarehouseDetail")
    @LogC(title = "库存出入库明细", businessType = BusinessType.EXPORT)
    public AjaxResult exportWMSWarehouseDetail(
            @RequestParam(required = false, defaultValue = "") String beginDate,
            @RequestParam(required = false, defaultValue = "") String endDate,
            @RequestParam(required = false, defaultValue = "") String skuClass,
            @RequestParam(required = false, defaultValue = "") String skuClassValue,
            @RequestParam(required = false, defaultValue = "") String wareHouseName,
            @RequestParam(required = false, defaultValue = "") String wareHouseValue,
            @RequestParam(required = false, defaultValue = "") String skuId,
            @RequestParam(required = false, defaultValue = "") String skuIdValue,
            @RequestParam(required = false, defaultValue = "") String batchNo,
            @RequestParam(required = false, defaultValue = "") String batchNoValue,
            Integer pageNum,Integer pageSize,
            HttpServletResponse response
    ){
        System.out.println(1);
        return warehouseService.exportWMSWarehouseDetail(beginDate, endDate, skuClass, skuClassValue, wareHouseName, wareHouseValue, skuId, skuIdValue, batchNo, batchNoValue,pageNum,pageSize,response);
    }
    /**
     * 出库明细导出
     * @param beginDate
     * @param endDate
     * @param skuClass
     * @param skuClassValue
     * @param wareHouseName
     * @param wareHouseValue
     * @param skuId
     * @param skuIdValue
     * @param batchNo
     * @param batchNoValue
     * @param response
     */
    @ApiOperation("导出pdf接口")
    @PostMapping("exportPdfWMSWarehouseDetail")
    @LogC(title = "库存出入库明细", businessType = BusinessType.EXPORTPDF)
    public AjaxResult exportPdfWMSWarehouseDetail(
            @RequestParam(required = false, defaultValue = "") String beginDate,
            @RequestParam(required = false, defaultValue = "") String endDate,
            @RequestParam(required = false, defaultValue = "") String skuClass,
            @RequestParam(required = false, defaultValue = "") String skuClassValue,
            @RequestParam(required = false, defaultValue = "") String wareHouseName,
            @RequestParam(required = false, defaultValue = "") String wareHouseValue,
            @RequestParam(required = false, defaultValue = "") String skuId,
            @RequestParam(required = false, defaultValue = "") String skuIdValue,
            @RequestParam(required = false, defaultValue = "") String batchNo,
            @RequestParam(required = false, defaultValue = "") String batchNoValue,
            Integer pageNum,Integer pageSize,
            HttpServletResponse response
    ){
        return warehouseService.exportPdfWMSWarehouseDetail(beginDate, endDate, skuClass, skuClassValue, wareHouseName, wareHouseValue, skuId, skuIdValue, batchNo, batchNoValue,pageNum,pageSize,response);
    }
    /**
     * 出库明细导出
     * @param beginDate
     * @param endDate
     * @param skuClass
     * @param skuClassValue
     * @param wareHouseName
     * @param wareHouseValue
     * @param skuId
     * @param skuIdValue
     * @param batchNo
     * @param batchNoValue
     * @param response
     */
    @ApiOperation("导出csv接口")
    @PostMapping("exportCsvWMSWarehouseDetail")
    @LogC(title = "库存出入库明细", businessType = BusinessType.EXPORTCSV)
    public AjaxResult exportCsvWMSWarehouseDetail(
            @RequestParam(required = false, defaultValue = "") String beginDate,
            @RequestParam(required = false, defaultValue = "") String endDate,
            @RequestParam(required = false, defaultValue = "") String skuClass,
            @RequestParam(required = false, defaultValue = "") String skuClassValue,
            @RequestParam(required = false, defaultValue = "") String wareHouseName,
            @RequestParam(required = false, defaultValue = "") String wareHouseValue,
            @RequestParam(required = false, defaultValue = "") String skuId,
            @RequestParam(required = false, defaultValue = "") String skuIdValue,
            @RequestParam(required = false, defaultValue = "") String batchNo,
            @RequestParam(required = false, defaultValue = "") String batchNoValue,
            Integer pageNum,Integer pageSize,
            HttpServletResponse response
    ){
        List<ExportWMSWarehouseOutDetailVo> list = warehouseService.exportCsvWMSWarehouseDetail(beginDate, endDate, skuClass, skuClassValue, wareHouseName, wareHouseValue, skuId, skuIdValue, batchNo, batchNoValue, pageNum,pageSize,response);
//        list.forEach(i -> i.setWms_create_time(DateUtil.format(DateUtil.parse(i.getWms_create_time(), "yyyy-MM-dd"),"yyyy-MM-dd")));
        try {
            CsvExportUtils.exportToWeb(response, "库存出入库明细.csv", list);
            return AjaxResult.error("导出成功");
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("导出失败");
        }
    }

    /**
     * 打印数据
     * @param vo
     * @return
     */
    @PostMapping("printSkuInfo")
    public R printSkuInfo(@RequestBody PrintSkuInfoVo vo){
        return warehouseService.printSkuInfo(vo);
    }

}